package com.heima.list;

import com.heima.array.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class ListWalk02 {
    public static void main(String[] args) {
        //学生类-已有
        //定义学生对象数组(二级引用) - 3个
        Student[] stuArr = new Student[3];//声明对象数组
        //键入学生姓名\年龄
        Scanner sc = new Scanner(System.in);
        getStudent(stuArr, sc);
        //集合存储并打印
        ArrayList<Student> stuList = new ArrayList<>();//一级引用
        listWalkPrint(stuArr, stuList);

    }

    public static void getStudent(Student[] stuArr, Scanner sc) {
        for (int i = 0; i < stuArr.length; i++) {
            System.out.println(stuArr[i]);
            stuArr[i] = new Student();//实例化对象数组
            System.out.println("请输入第"+(i +1)+"个学生的姓名");
            //????????????????空指针???????????????????????
            //已解决,对引用类型new完为空指针,不能用方法,只能用构造函数
//            System.out.println(stuArr);
//            System.out.println(stuArr[i]);
            stuArr[i].setName(sc.next());
            System.out.println("请输入第"+(i +1)+"个学生的年龄");
            stuArr[i].setAge(sc.nextInt());
        }
    }

    public static void listWalkPrint(Student[] stuArr, ArrayList<Student> stuList) {
        for (int i = 0; i < stuArr.length; i++) {
            stuList.add(i, stuArr[i]);//存入
            //遍历并打印
            System.out.println("第"+(i+1)+"个学生:"+ stuList.get(i).getName()+"的年龄为"+ stuList.get(i).getAge());
        }
    }

}
